package com.wj.springboot2demo.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wj.springboot2demo.domain.ho.UserMapper;
import com.wj.springboot2demo.domain.model.TbSearchManagerUser;
import com.wj.springboot2demo.domain.model.TbSearchManagerUserExample;
import com.wj.springboot2demo.domain.model.UserExample;
import com.wj.springboot2demo.domain.searchmanager.TbSearchManagerUserMapper;
import com.wj.springboot2demo.mq.RabbitMqProducer;

@RestController
public class FirstController {

    /*
     * @RequestParam 注解有四个参数，均为非必填：
     *      defaultValue 指定了参数的默认值
     *      required 参数是否必传
     *      value和name参数互为别名，可以同时指定，但是值必须一致，否则报错：
     */
    @RequestMapping("/springboot2/index")
    public String index(@RequestParam(defaultValue = "defaultV", value = "value") String v
            ,@RequestParam(defaultValue = "defaultV", value = "value2") String v2) {
        return "first springboot 2 controller : " + v + ", v2 : " + v2;
    }
    
    @RequestMapping("/springboot2/mysql")
    public String mysql(@RequestParam(defaultValue = "defaultV", value = "value") String v
            ,@RequestParam(defaultValue = "defaultV", value = "value2") String v2) {
        TbSearchManagerUserExample example = new TbSearchManagerUserExample();
        
        int count = tbSearchManagerUserMapper.countByExample(example);
        System.out.println(count + " > " + v);
        
        example.createCriteria().andUserIdEqualTo(2L);
        
        TbSearchManagerUser record = new TbSearchManagerUser();
        record.setNickName("SpringBoot");
        
        int rs = tbSearchManagerUserMapper.updateByExampleSelective(record, example);
        System.out.println("searchManagerUser count : " + rs);
        
        return "count : " + count + " > " + v;
    }

    @RequestMapping("/springboot2/mysql1")
    public String mysql1(@RequestParam(defaultValue = "defaultV", value = "value") String v
            ,@RequestParam(defaultValue = "defaultV", value = "value2") String v2) {
        
        UserExample userExample = new UserExample();
        System.out.println("user count : " + userMapper.countByExample(userExample));
        
        return "count : ok";
    }
    
    @RequestMapping("/springboot2/rabbit/p")
    public String rabbitProducer(@RequestParam(defaultValue = "defaultV", value = "value") String v
            ,@RequestParam(defaultValue = "defaultV", value = "value2") String v2) {
        
        Map<String, Object> msg = new HashMap<String, Object>();
        
        msg.put("msg_id", "1111111");
        msg.put("msg_millis", System.currentTimeMillis());

        rabbitMqProducer.send(v + "_message", msg);
        
        return "count : ok";
    }
    
    
    @Autowired
    private UserMapper userMapper;
    
    @Autowired
    private TbSearchManagerUserMapper tbSearchManagerUserMapper;
    
    @Autowired
    private RabbitMqProducer rabbitMqProducer; 
}

